﻿using System;
using System.Linq;
using System.Text;
using Biz.EF.Gateway.Total;
using Biz.EF.Platform.Gxt;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class ProcessorDownlineCmd
    {
        private static ILog Log = LogManager.GetLogger(typeof(ProcessorDownlineCmd));

        public static void GenDownlineCmd()
        {
            var sb = new StringBuilder();
            try
            {
                using (var gCtx = new GatewayTotalContext())
                using (var pCtx = new PlatformGxtContext())
                {
                    var items =
                        pCtx.gateway_device_ctrl
//.Where(t => (t.QNrtn==null|| t.QNrtn.Trim()==string.Empty) && (t.ExeRtn == null || t.ExeRtn.Trim() == string.Empty))
//凯发.Where(t => (string.IsNullOrEmpty(t.QNrtn) && string.IsNullOrEmpty(t.ExeRtn)) || (t.QNrtn.Equals("-") && t.Result.Equals("控制器离线")))
.Where(t =>!( "Downloaded".Equals(t.remark)))
.Take(100)
                        .ToList();
                    foreach (var item in items)
                    {
                        var now = DateTime.Now;
                        sb.Length = 0;
                        if (string.IsNullOrEmpty(item.QN) || item.QN.Trim().Length != 17)
                        {
                            item.QN = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                        }
                        sb.Append("QN=" + item.QN + ";");
                        sb.Append("ST=32;");
                        if (item.CtrlCode.Equals("3098"))
                        {
                            item.CtrlCode = "3097";
                        }
                        sb.Append("CN=" + item.CtrlCode + ";");
                        sb.Append("PW=123456;");
                        sb.Append("MN=" + item.MN + ";");
                        sb.Append("Flag=1;");
                        string cp = "";
                        switch (item.CtrlCode)
                        {
                            case "3051":
                                if (item.Param != null) cp = "Per=" + item.Param.Value.ToString("f0");
                                break;
                            case "3097":
                                var gate = "STATUS";
                                if (item.Param != null)
                                {
                                    if (Math.Abs(item.Param.Value - 100) <= 0.001)
                                    {
                                        gate = "ON";
                                    }
                                    else if (Math.Abs(item.Param.Value - 0) <= 0.001)
                                    {
                                        gate = "OFF";
                                    }
                                    cp = "Gate=" + gate;
                                }
                                break;
                            default:
                                break;
                        }
                        sb.Append("CP=&&" + cp + "&&");
                        send_ctr_cmd data = new send_ctr_cmd
                        {
                            cn = item.CtrlCode,
                            is_sent = false,
                            mn = item.MN,
                            qn = item.QN,
                            send_string = Crc.WrapByCrc(sb.ToString(), "total"),
                            send_time = now,
                            sent_time = now
                        };
                        Log.Info("Downline message processing:" + data.send_string);
                        gCtx.send_ctr_cmd.Add(data);
                        item.Result = "控制器离线";
                        item.QNrtn = "D";
                        item.ExeRtn = "D";
                        item.remark = "Downloaded";
                    }
                    gCtx.SaveChanges();
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
    }
}
